<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="18">
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          :rules="rules"
          style="margin-left: 130px"
        >
          <el-form-item label="帮助说明编码：" prop="helpCode">
            <el-select
              v-model="form.helpCode"
              placeholder="请选择"
              size="small"
              style="width: 325px"
            >
              <el-option
                v-for="item in helpCodeSelection"
                :key="item.optCode"
                :label="item.optName"
                :value="item.optCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="一级菜单名称：" prop="firstMenu">
            <el-input
              v-model="form.firstMenu"
              size="small"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="输入端口：" prop="inPort">
            <el-input v-model="form.inPort" size="small" clearable></el-input>
          </el-form-item>
          <el-form-item label="输出端口：" prop="outPort">
            <el-input v-model="form.outPort" size="small" clearable></el-input>
          </el-form-item>
          <el-form-item label="功能说明：" prop="funDescription">
            <el-input
              type="textarea"
              v-model="form.funDescription"
              :autosize="{ minRows: 2, maxRows: 8 }"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数：" prop="functionName">
            <el-input
              type="textarea"
              v-model="form.functionName"
              :autosize="{ minRows: 2, maxRows: 8 }"
            ></el-input>
          </el-form-item>
          <el-form-item class="form-btn-row">
            <mine-button type="primary" @click="onCancel" secondary
              >取 消
            </mine-button>
            <mine-button type="primary" @click="onSubmit">保 存 </mine-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import request from "@/modules/index/utils/request";
import qs from "qs";
import { QWebChannel } from "@/modules/index/utils/qwebchannel.js";

export default {
  name: "addHelp",
  props: {
    rowData: {
      type: Object,
    },
  },
  data() {
    return {
      form: {},
      rules: {},
      helpCodeSelection: [], // 帮助说明编码下拉选项
    };
  },
  watch: {
    // 编辑时回显
    "rowData.id": {
      handler(val) {
        this.form = this.rowData;
      },
      immediate: true,
    },
  },
  methods: {
    async onSubmit() {
      let res = {};
      // 如果props中rowData有值，则走编辑接口
      if (this.rowData.id) {
        let _self = this;
        if (_self.$qwebUtil.isQtClient) {
          const uuid = _self.$qwebUtil.generateRandomUUID();
          let sendObject;
          /* eslint-disable-next-line */
          const channel = new QWebChannel(
            window.qt.webChannelTransport,
            async function (channel) {
              sendObject = channel.objects.sendObject;
              // receivedJSON 修改此处发送请求参数
              const receivedJSON = {
                RequestUrl: "/helpInfo/update",
                RequestMethod: "post",
                ContentType: "application/json;charset=UTF-8",
                RequestBody: qs.stringify(_self.form, { indices: false }),
                isStringParam: true,
                requestId: uuid,
              };
              await send();
              await _self.$qwebUtil.receive(sendObject, receivedJSON);
              // console.error('received','/feCanvas/pageList')
            }
          );
          /* eslint-disable-next-line */
          async function send() {
            sendObject.sendMessage.connect(function (data) {
              if (!data) {
                console.error("No data received.");
              }
              res = JSON.parse(data);
              const result = res.response || {};
              // update the followings
              if (result.success && res.webInfo.requestId == uuid) {
                _self.$message.success(result.msg);
                _self.$emit("changeFlag");
              } else {
                _self.$message.warning("保存失败！");
              }
            });
          }
        } else if (this.$webview2.isWPF) {
          const uuid = this.$webview2.generateRandomUUID();
          await this.$webview2.sendWebView2(
            "/helpInfo/update",
            "post",
            "application/json;charset=UTF-8",
            qs.stringify(this.form, { indices: false }),
            true,
            uuid
          );
          await window.chrome.webview.addEventListener(
            "message",
            async (e) => {
              let res = e.data.response;
              if (res.status === 200) {
                this.$message.success(res.msg);
                this.$emit("changeFlag");
              } else {
                this.$message.warning("保存失败！");
              }
            },
            { once: true }
          );
        } else {
          res = await this.$store.dispatch(
            "helpInfo/helpInfoUpdate",
            this.form
          );
          if (res.data.status === 200) {
            this.$message.success(res.data.msg);
            this.$emit("changeFlag");
          } else {
            this.$message.warning("保存失败！");
          }
        }
      } else {
        let res = {};
        let _self = this;
        if (_self.$qwebUtil.isQtClient) {
          const uuid = _self.$qwebUtil.generateRandomUUID();
          let sendObject;
          /* eslint-disable-next-line */
          const channel = new QWebChannel(
            window.qt.webChannelTransport,
            async function (channel) {
              sendObject = channel.objects.sendObject;
              // receivedJSON 修改此处发送请求参数
              const receivedJSON = {
                RequestUrl: "/helpInfo/save",
                RequestMethod: "post",
                ContentType: "application/json;charset=UTF-8",
                RequestBody: qs.stringify(_self.form, { indices: false }),
                isStringParam: true,
                requestId: uuid,
              };
              await send();
              await _self.$qwebUtil.receive(sendObject, receivedJSON);
              // console.error('received','/feCanvas/pageList')
            }
          );
          /* eslint-disable-next-line */
          async function send() {
            sendObject.sendMessage.connect(function (data) {
              if (!data) {
                console.error("No data received.");
              }
              res = JSON.parse(data);
              const result = res.response || {};
              // update the followings
              if (result.success && res.webInfo.requestId == uuid) {
                _self.$message.success(result.msg);
                _self.$emit("changeFlag");
              } else {
                _self.$message.warning("保存失败！");
              }
            });
          }
        } else if (this.$webview2.isWPF) {
          const uuid = this.$webview2.generateRandomUUID();
          await this.$webview2.sendWebView2(
            "/helpInfo/save",
            "post",
            "application/json;charset=UTF-8",
            qs.stringify(this.form, { indices: false }),
            true,
            uuid
          );
          await window.chrome.webview.addEventListener(
            "message",
            async (e) => {
              let res = e.data.response;
              if (res.status === 200) {
                this.$message.success(res.msg);
                this.$emit("changeFlag");
              } else {
                this.$message.warning("保存失败！");
              }
            },
            { once: true }
          );
        } else {
          res = await this.$store.dispatch("helpInfo/helpInfoSave", this.form);
          if (res.data.status === 200) {
            this.$message.success(res.data.msg);
            this.$emit("changeFlag");
          } else {
            this.$message.warning("保存失败！");
          }
        }
      }
    },
    onCancel() {
      this.form = {};
      this.$emit("changeFlag");
    },
    async getHelpCodeSelection() {
      let res = {};
      let _self = this;
      if (_self.$qwebUtil.isQtClient) {
        const uuid = _self.$qwebUtil.generateRandomUUID();
        let sendObject;
        /* eslint-disable-next-line */
        const channel = new QWebChannel(
          window.qt.webChannelTransport,
          async function (channel) {
            sendObject = channel.objects.sendObject;
            // receivedJSON 修改此处发送请求参数
            const receivedJSON = {
              RequestUrl: "/dictionary/TreeList",
              RequestMethod: "get",
              ContentType: "application/json;charset=UTF-8",
              RequestBody: qs.stringify(
                {
                  dirCode: "use_help",
                },
                { indices: false }
              ),
              isStringParam: true,
              requestId: uuid,
            };
            await send();
            await _self.$qwebUtil.receive(sendObject, receivedJSON);
            // console.error('received','/feCanvas/pageList')
          }
        );
        /* eslint-disable-next-line */
        async function send() {
          sendObject.sendMessage.connect(function (data) {
            if (!data) {
              console.error("No data received.");
            }
            res = JSON.parse(data);
            const result = res.response || {};
            // update the followings
            if (result.success && res.webInfo.requestId == uuid) {
              _self.helpCodeSelection = result?.data[0]?.childrenParams;
            } else {
              _self.$message.warning("获取数据失败");
            }
          });
        }
      } else if (this.$webview2.isWPF) {
        const uuid = this.$webview2.generateRandomUUID();
        await this.$webview2.sendWebView2(
          "/dictionary/TreeList",
          "get",
          "application/json;charset=UTF-8",
          qs.stringify(
            {
              dirCode: "use_help",
            },
            { indices: false }
          ),
          true,
          uuid
        );
        await window.chrome.webview.addEventListener(
          "message",
          (e) => {
            let res = e.data.response;
            if (res.status === 200) {
              this.helpCodeSelection = res?.data[0]?.childrenParams;
            } else {
              this.$message.warning("获取数据失败");
            }
          },
          { once: true }
        );
      } else {
        const res = await this.$store.dispatch("getDicBydirCode", {
          dirCode: "use_help",
        });
        if (res.data.success) {
          this.helpCodeSelection = res.data?.data[0]?.childrenParams;
        }
      }
    },
  },
  created() {
    this.getHelpCodeSelection();
  },
};
</script>

<style scoped>
</style>